<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%!
	public static List<String> getBeforeDays(String dateStr, int num){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;;
		try {
			date = simpleDateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<String> list = new ArrayList<String>();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		list.add(DateUtil.formatDate(cal.getTime()));
		for(int i = 0;i<num - 1;i++) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
			list.add(DateUtil.formatDate(cal.getTime()));
		}
		Collections.reverse(list);
		return list;
	}
	
	public static String getBeforeDaysString(String dateStr, int num){
		List<String> list = getBeforeDays(dateStr, num);
		String str = "";
		for (String d:list) {
			str += ",'" + d + "'";
		}
		if (list.size() == 0) {
			return "('-1')";
		}
		str = "(" + str.substring(1) + ")";
		return str;
	}
%>
<%
	Integer uid = input.getInt("uid");
	String date = input.getDesc("date", "");
	if (date.equals("")) {
		date = DateUtil.formatDate(new Date());
	}
	List<Map> ranks = QBuilder.db("basedb").select("new map( step as step, step_time as date)").from("steps").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("uid=", uid).and("step_time in" + getBeforeDaysString(date, 7))).order("step_time", "desc").query().getResultObj();	
	List<String> list = getBeforeDays(date, 7);
	List<String> existed = new ArrayList<String>();
	for (Map map:ranks) {
		existed.add(Tools.getDesc(map.get("date")));
	}
	list.removeAll(existed);
	for (String string:list) {
		Map map = new HashMap();
		map.put("date", string);
		map.put("step", 0);
		ranks.add(map);
	}
	Collections.sort(ranks, new Comparator<Map>() {
		public int compare(Map o1, Map o2) {
			return Tools.getDesc(o1.get("date")).compareTo(Tools.getDesc(o2.get("date")));
		}
	});
	msg.put("data", ranks);
	Tools.encryptPrint(out, msg.toJson());
%>